home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / Wave / WavesWorld / Source / Libraries / tcl7.4b3 / tclCmdIL.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-10  |  36.5 KB  |  1,428 lines

  1. /* 
  2.  * tclCmdIL.c --
  3.  *
  4.  *    This file contains the top-level command routines for most of
  5.  *    the Tcl built-in commands whose names begin with the letters
  6.  *    I through L.  It contains only commands in the generic core
  7.  *    (i.e. those that don't depend much upon UNIX facilities).
  8.  *
  9.  * Copyright (c) 1987-1993 The Regents of the University of California.
  10.  * Copyright (c) 1994-1995 Sun Microsystems, Inc.
  11.  *
  12.  * See the file "license.terms" for information on usage and redistribution
  13.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  14.  */
  15.  
  16. #ifndef lint
  17. static char sccsid[] = "@(#) tclCmdIL.c 1.108 95/01/10 09:26:55";
  18. #endif
  19.  
  20. #include "tclInt.h"
  21. #include "tclPort.h"
  22. #include "patchlevel.h"
  23.  
  24. /*
  25.  * The variables below are used to implement the "lsort" command.
  26.  * Unfortunately, this use of static variables prevents "lsort"
  27.  * from being thread-safe, but there's no alternative given the
  28.  * current implementation of qsort.  In a threaded environment
  29.  * these variables should be made thread-local if possible, or else
  30.  * "lsort" needs internal mutual exclusion.
  31.  */
  32.  
  33. static Tcl_Interp *sortInterp;        /* Interpreter for "lsort" command. */
  34. static enum {ASCII, INTEGER, REAL, COMMAND} sortMode;
  35.                     /* Mode for sorting: compare as strings,
  36.                      * compare as numbers, or call
  37.                      * user-defined command for
  38.                      * comparison. */
  39. static Tcl_DString sortCmd;        /* Holds command if mode is COMMAND.
  40.                      * pre-initialized to hold base of
  41.                      * command. */
  42. static int sortIncreasing;        /* 0 means sort in decreasing order,
  43.                      * 1 means increasing order. */
  44. static int sortCode;            /* Anything other than TCL_OK means a
  45.                      * problem occurred while sorting; this
  46.                      * executing a comparison command, so
  47.                      * the sort was aborted. */
  48.  
  49. /*
  50.  * Forward declarations for procedures defined in this file:
  51.  */
  52.  
  53. static int        SortCompareProc _ANSI_ARGS_((CONST VOID *first,
  54.                 CONST VOID *second));
  55.  
  56. /*
  57.  *----------------------------------------------------------------------
  58.  *
  59.  * Tcl_IfCmd --
  60.  *
  61.  *    This procedure is invoked to process the "if" Tcl command.
  62.  *    See the user documentation for details on what it does.
  63.  *
  64.  * Results:
  65.  *    A standard Tcl result.
  66.  *
  67.  * Side effects:
  68.  *    See the user documentation.
  69.  *
  70.  *----------------------------------------------------------------------
  71.  */
  72.  
  73.     /* ARGSUSED */
  74. int
  75. Tcl_IfCmd(dummy, interp, argc, argv)
  76.     ClientData dummy;            /* Not used. */
  77.     Tcl_Interp *interp;            /* Current interpreter. */
  78.     int argc;                /* Number of arguments. */
  79.     char **argv;            /* Argument strings. */
  80. {
  81.     int i, result, value;
  82.  
  83.     i = 1;
  84.     while (1) {
  85.     /*
  86.      * At this point in the loop, argv and argc refer to an expression
  87.      * to test, either for the main expression or an expression
  88.      * following an "elseif".  The arguments after the expression must
  89.      * be "then" (optional) and a script to execute if the expression is
  90.      * true.
  91.      */
  92.  
  93.     if (i >= argc) {
  94.         Tcl_AppendResult(interp, "wrong # args: no expression after \"",
  95.             argv[i-1], "\" argument", (char *) NULL);
  96.         return TCL_ERROR;
  97.     }
  98.     result = Tcl_ExprBoolean(interp, argv[i], &value);
  99.     if (result != TCL_OK) {
  100.         return result;
  101.     }
  102.     i++;
  103.     if ((i < argc) && (strcmp(argv[i], "then") == 0)) {
  104.         i++;
  105.     }
  106.     if (i >= argc) {
  107.         Tcl_AppendResult(interp, "wrong # args: no script following \"",
  108.             argv[i-1], "\" argument", (char *) NULL);
  109.         return TCL_ERROR;
  110.     }
  111.     if (value) {
  112.         return Tcl_Eval(interp, argv[i]);
  113.     }
  114.  
  115.     /*
  116.      * The expression evaluated to false.  Skip the command, then
  117.      * see if there is an "else" or "elseif" clause.
  118.      */
  119.  
  120.     i++;
  121.     if (i >= argc) {
  122.         return TCL_OK;
  123.     }
  124.     if ((argv[i][0] == 'e') && (strcmp(argv[i], "elseif") == 0)) {
  125.         i++;
  126.         continue;
  127.     }
  128.     break;
  129.     }
  130.  
  131.     /*
  132.      * Couldn't find a "then" or "elseif" clause to execute.  Check now
  133.      * for an "else" clause.  We know that there's at least one more
  134.      * argument when we get here.
  135.      */
  136.  
  137.     if (strcmp(argv[i], "else") == 0) {
  138.     i++;
  139.     if (i >= argc) {
  140.         Tcl_AppendResult(interp,
  141.             "wrong # args: no script following \"else\" argument",
  142.             (char *) NULL);
  143.         return TCL_ERROR;
  144.     }
  145.     }
  146.     return Tcl_Eval(interp, argv[i]);
  147. }
  148.  
  149. /*
  150.  *----------------------------------------------------------------------
  151.  *
  152.  * Tcl_IncrCmd --
  153.  *
  154.  *    This procedure is invoked to process the "incr" Tcl command.
  155.  *    See the user documentation for details on what it does.
  156.  *
  157.  * Results:
  158.  *    A standard Tcl result.
  159.  *
  160.  * Side effects:
  161.  *    See the user documentation.
  162.  *
  163.  *----------------------------------------------------------------------
  164.  */
  165.  
  166.     /* ARGSUSED */
  167. int
  168. Tcl_IncrCmd(dummy, interp, argc, argv)
  169.     ClientData dummy;            /* Not used. */
  170.     Tcl_Interp *interp;            /* Current interpreter. */
  171.     int argc;                /* Number of arguments. */
  172.     char **argv;            /* Argument strings. */
  173. {
  174.     int value;
  175.     char *oldString, *result;
  176.     char newString[30];
  177.  
  178.     if ((argc != 2) && (argc != 3)) {
  179.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  180.         " varName ?increment?\"", (char *) NULL);
  181.     return TCL_ERROR;
  182.     }
  183.  
  184.     oldString = Tcl_GetVar(interp, argv[1], TCL_LEAVE_ERR_MSG);
  185.     if (oldString == NULL) {
  186.     return TCL_ERROR;
  187.     }
  188.     if (Tcl_GetInt(interp, oldString, &value) != TCL_OK) {
  189.     Tcl_AddErrorInfo(interp,
  190.         "\n    (reading value of variable to increment)");
  191.     return TCL_ERROR;
  192.     }
  193.     if (argc == 2) {
  194.     value += 1;
  195.     } else {
  196.     int increment;
  197.  
  198.     if (Tcl_GetInt(interp, argv[2], &increment) != TCL_OK) {
  199.         Tcl_AddErrorInfo(interp,
  200.             "\n    (reading increment)");
  201.         return TCL_ERROR;
  202.     }
  203.     value += increment;
  204.     }
  205.     sprintf(newString, "%d", value);
  206.     result = Tcl_SetVar(interp, argv[1], newString, TCL_LEAVE_ERR_MSG);
  207.     if (result == NULL) {
  208.     return TCL_ERROR;
  209.     }
  210.     interp->result = result;
  211.     return TCL_OK; 
  212. }
  213.  
  214. /*
  215.  *----------------------------------------------------------------------
  216.  *
  217.  * Tcl_InfoCmd --
  218.  *
  219.  *    This procedure is invoked to process the "info" Tcl command.
  220.  *    See the user documentation for details on what it does.
  221.  *
  222.  * Results:
  223.  *    A standard Tcl result.
  224.  *
  225.  * Side effects:
  226.  *    See the user documentation.
  227.  *
  228.  *----------------------------------------------------------------------
  229.  */
  230.  
  231.     /* ARGSUSED */
  232. int
  233. Tcl_InfoCmd(dummy, interp, argc, argv)
  234.     ClientData dummy;            /* Not used. */
  235.     Tcl_Interp *interp;            /* Current interpreter. */
  236.     int argc;                /* Number of arguments. */
  237.     char **argv;            /* Argument strings. */
  238. {
  239.     register Interp *iPtr = (Interp *) interp;
  240.     size_t length;
  241.     int c;
  242.     Arg *argPtr;
  243.     Proc *procPtr;
  244.     Var *varPtr;
  245.     Command *cmdPtr;
  246.     Tcl_HashEntry *hPtr;
  247.     Tcl_HashSearch search;
  248.  
  249.     if (argc < 2) {
  250.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  251.         " option ?arg arg ...?\"", (char *) NULL);
  252.     return TCL_ERROR;
  253.     }
  254.     c = argv[1][0];
  255.     length = strlen(argv[1]);
  256.     if ((c == 'a') && (strncmp(argv[1], "args", length)) == 0) {
  257.     if (argc != 3) {
  258.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  259.             argv[0], " args procname\"", (char *) NULL);
  260.         return TCL_ERROR;
  261.     }
  262.     procPtr = TclFindProc(iPtr, argv[2]);
  263.     if (procPtr == NULL) {
  264.         infoNoSuchProc:
  265.         Tcl_AppendResult(interp, "\"", argv[2],
  266.             "\" isn't a procedure", (char *) NULL);
  267.         return TCL_ERROR;
  268.     }
  269.     for (argPtr = procPtr->argPtr; argPtr != NULL;
  270.         argPtr = argPtr->nextPtr) {
  271.         Tcl_AppendElement(interp, argPtr->name);
  272.     }
  273.     return TCL_OK;
  274.     } else if ((c == 'b') && (strncmp(argv[1], "body", length)) == 0) {
  275.     if (argc != 3) {
  276.         Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  277.             " body procname\"", (char *) NULL);
  278.         return TCL_ERROR;
  279.     }
  280.     procPtr = TclFindProc(iPtr, argv[2]);
  281.     if (procPtr == NULL) {
  282.         goto infoNoSuchProc;
  283.     }
  284.     iPtr->result = procPtr->command;
  285.     return TCL_OK;
  286.     } else if ((c == 'c') && (strncmp(argv[1], "cmdcount", length) == 0)
  287.         && (length >= 2)) {
  288.     if (argc != 2) {
  289.         Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  290.             " cmdcount\"", (char *) NULL);
  291.         return TCL_ERROR;
  292.     }
  293.     sprintf(iPtr->result, "%d", iPtr->cmdCount);
  294.     return TCL_OK;
  295.     } else if ((c == 'c') && (strncmp(argv[1], "commands", length) == 0)
  296.         && (length >= 4)) {
  297.     if (argc > 3) {
  298.         Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  299.             " commands [pattern]\"", (char *) NULL);
  300.         return TCL_ERROR;
  301.     }
  302.     for (hPtr = Tcl_FirstHashEntry(&iPtr->commandTable, &search);
  303.         hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
  304.         char *name = Tcl_GetHashKey(&iPtr->commandTable, hPtr);
  305.         if ((argc == 3) && !Tcl_StringMatch(name, argv[2])) {
  306.         continue;
  307.         }
  308.         Tcl_AppendElement(interp, name);
  309.     }
  310.     return TCL_OK;
  311.     } else if ((c == 'c') && (strncmp(argv[1], "complete", length) == 0)
  312.         && (length >= 4)) {
  313.     if (argc != 3) {
  314.         Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  315.             " complete command\"", (char *) NULL);
  316.         return TCL_ERROR;
  317.     }
  318.     if (Tcl_CommandComplete(argv[2])) {
  319.         interp->result = "1";
  320.     } else {
  321.         interp->result = "0";
  322.     }
  323.     return TCL_OK;
  324.     } else if ((c == 'd') && (strncmp(argv[1], "default", length)) == 0) {
  325.     if (argc != 5) {
  326.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  327.             argv[0], " default procname arg varname\"",
  328.             (char *) NULL);
  329.         return TCL_ERROR;
  330.     }
  331.     procPtr = TclFindProc(iPtr, argv[2]);
  332.     if (procPtr == NULL) {
  333.         goto infoNoSuchProc;
  334.     }
  335.     for (argPtr = procPtr->argPtr; ; argPtr = argPtr->nextPtr) {
  336.         if (argPtr == NULL) {
  337.         Tcl_AppendResult(interp, "procedure \"", argv[2],
  338.             "\" doesn't have an argument \"", argv[3],
  339.             "\"", (char *) NULL);
  340.         return TCL_ERROR;
  341.         }
  342.         if (strcmp(argv[3], argPtr->name) == 0) {
  343.         if (argPtr->defValue != NULL) {
  344.             if (Tcl_SetVar((Tcl_Interp *) iPtr, argv[4],
  345.                 argPtr->defValue, 0) == NULL) {
  346.             defStoreError:
  347.             Tcl_AppendResult(interp,
  348.                 "couldn't store default value in variable \"",
  349.                 argv[4], "\"", (char *) NULL);
  350.             return TCL_ERROR;
  351.             }
  352.             iPtr->result = "1";
  353.         } else {
  354.             if (Tcl_SetVar((Tcl_Interp *) iPtr, argv[4], "", 0)
  355.                 == NULL) {
  356.             goto defStoreError;
  357.             }
  358.             iPtr->result = "0";
  359.         }
  360.         return TCL_OK;
  361.         }
  362.     }
  363.     } else if ((c == 'e') && (strncmp(argv[1], "exists", length) == 0)) {
  364.     char *p;
  365.     if (argc != 3) {
  366.         Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  367.             " exists varName\"", (char *) NULL);
  368.         return TCL_ERROR;
  369.     }
  370.     p = Tcl_GetVar((Tcl_Interp *) iPtr, argv[2], 0);
  371.  
  372.     /*
  373.      * The code below handles the special case where the name is for
  374.      * an array:  Tcl_GetVar will reject this since you can't read
  375.      * an array variable without an index.
  376.      */
  377.  
  378.     if (p == NULL) {
  379.         Tcl_HashEntry *hPtr;
  380.         Var *varPtr;
  381.  
  382.         if (strchr(argv[2], '(') != NULL) {
  383.         noVar:
  384.         iPtr->result = "0";
  385.         return TCL_OK;
  386.         }
  387.         if (iPtr->varFramePtr == NULL) {
  388.         hPtr = Tcl_FindHashEntry(&iPtr->globalTable, argv[2]);
  389.         } else {
  390.         hPtr = Tcl_FindHashEntry(&iPtr->varFramePtr->varTable, argv[2]);
  391.         }
  392.         if (hPtr == NULL) {
  393.         goto noVar;
  394.         }
  395.         varPtr = (Var *) Tcl_GetHashValue(hPtr);
  396.         if (varPtr->flags & VAR_UPVAR) {
  397.         varPtr = varPtr->value.upvarPtr;
  398.         }
  399.         if (!(varPtr->flags & VAR_ARRAY)) {
  400.         goto noVar;
  401.         }
  402.     }
  403.     iPtr->result = "1";
  404.     return TCL_OK;
  405.     } else if ((c == 'g') && (strncmp(argv[1], "globals", length) == 0)) {
  406.     char *name;
  407.  
  408.     if (argc > 3) {
  409.         Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  410.             " globals [pattern]\"", (char *) NULL);
  411.         return TCL_ERROR;
  412.     }
  413.     for (hPtr = Tcl_FirstHashEntry(&iPtr->globalTable, &search);
  414.         hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
  415.         varPtr = (Var *) Tcl_GetHashValue(hPtr);
  416.         if (varPtr->flags & VAR_UNDEFINED) {
  417.         continue;
  418.         }
  419.         name = Tcl_GetHashKey(&iPtr->globalTable, hPtr);
  420.         if ((argc == 3) && !Tcl_StringMatch(name, argv[2])) {
  421.         continue;
  422.         }
  423.         Tcl_AppendElement(interp, name);
  424.     }
  425.     return TCL_OK;
  426.     } else if ((c == 'l') && (strncmp(argv[1], "level", length) == 0)
  427.         && (length >= 2)) {
  428.     if (argc == 2) {
  429.         if (iPtr->varFramePtr == NULL) {
  430.         iPtr->result = "0";
  431.         } else {
  432.         sprintf(iPtr->result, "%d", iPtr->varFramePtr->level);
  433.         }
  434.         return TCL_OK;
  435.     } else if (argc == 3) {
  436.         int level;
  437.         CallFrame *framePtr;
  438.  
  439.         if (Tcl_GetInt(interp, argv[2], &level) != TCL_OK) {
  440.         return TCL_ERROR;
  441.         }
  442.         if (level <= 0) {
  443.         if (iPtr->varFramePtr == NULL) {
  444.             levelError:
  445.             Tcl_AppendResult(interp, "bad level \"", argv[2],
  446.                 "\"", (char *) NULL);
  447.             return TCL_ERROR;
  448.         }
  449.         level += iPtr->varFramePtr->level;
  450.         }
  451.         for (framePtr = iPtr->varFramePtr; framePtr != NULL;
  452.             framePtr = framePtr->callerVarPtr) {
  453.         if (framePtr->level == level) {
  454.             break;
  455.         }
  456.         }
  457.         if (framePtr == NULL) {
  458.         goto levelError;
  459.         }
  460.         iPtr->result = Tcl_Merge(framePtr->argc, framePtr->argv);
  461.         iPtr->freeProc = (Tcl_FreeProc *) free;
  462.         return TCL_OK;
  463.     }
  464.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  465.         " level [number]\"", (char *) NULL);
  466.     return TCL_ERROR;
  467.     } else if ((c == 'l') && (strncmp(argv[1], "library", length) == 0)
  468.         && (length >= 2)) {
  469.     if (argc != 2) {
  470.         Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  471.             " library\"", (char *) NULL);
  472.         return TCL_ERROR;
  473.     }
  474.     interp->result = getenv("TCL_LIBRARY");
  475.     if (interp->result == NULL) {
  476. #ifdef TCL_LIBRARY
  477.         interp->result = TCL_LIBRARY;
  478. #else
  479.         interp->result = "there is no Tcl library at this installation";
  480.         return TCL_ERROR;
  481. #endif
  482.     }
  483.     return TCL_OK;
  484.     } else if ((c == 'l') && (strncmp(argv[1], "locals", length) == 0)
  485.         && (length >= 2)) {
  486.     char *name;
  487.  
  488.     if (argc > 3) {
  489.         Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  490.             " locals [pattern]\"", (char *) NULL);
  491.         return TCL_ERROR;
  492.     }
  493.     if (iPtr->varFramePtr == NULL) {
  494.         return TCL_OK;
  495.     }
  496.     for (hPtr = Tcl_FirstHashEntry(&iPtr->varFramePtr->varTable, &search);
  497.         hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
  498.         varPtr = (Var *) Tcl_GetHashValue(hPtr);
  499.         if (varPtr->flags & (VAR_UNDEFINED|VAR_UPVAR)) {
  500.         continue;
  501.         }
  502.         name = Tcl_GetHashKey(&iPtr->varFramePtr->varTable, hPtr);
  503.         if ((argc == 3) && !Tcl_StringMatch(name, argv[2])) {
  504.         continue;
  505.         }
  506.         Tcl_AppendElement(interp, name);
  507.     }
  508.     return TCL_OK;
  509.     } else if ((c == 'p') && (strncmp(argv[1], "patchlevel", length) == 0)
  510.         && (length >= 2)) {
  511.     if (argc != 2) {
  512.         Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  513.             " patchlevel\"", (char *) NULL);
  514.         return TCL_ERROR;
  515.     }
  516.     interp->result = TCL_PATCH_LEVEL;
  517.     return TCL_OK;
  518.     } else if ((c == 'p') && (strncmp(argv[1], "procs", length) == 0)
  519.         && (length >= 2)) {
  520.     if (argc > 3) {
  521.         Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  522.             " procs [pattern]\"", (char *) NULL);
  523.         return TCL_ERROR;
  524.     }
  525.     for (hPtr = Tcl_FirstHashEntry(&iPtr->commandTable, &search);
  526.         hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
  527.         char *name = Tcl_GetHashKey(&iPtr->commandTable, hPtr);
  528.  
  529.         cmdPtr = (Command *) Tcl_GetHashValue(hPtr);
  530.         if (!TclIsProc(cmdPtr)) {
  531.         continue;
  532.         }
  533.         if ((argc == 3) && !Tcl_StringMatch(name, argv[2])) {
  534.         continue;
  535.         }
  536.         Tcl_AppendElement(interp, name);
  537.     }
  538.     return TCL_OK;
  539.     } else if ((c == 's') && (strncmp(argv[1], "script", length) == 0)) {
  540.     if (argc != 2) {
  541.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  542.             argv[0], " script\"", (char *) NULL);
  543.         return TCL_ERROR;
  544.     }
  545.     if (iPtr->scriptFile != NULL) {
  546.         /*
  547.          * Can't depend on iPtr->scriptFile to be non-volatile:
  548.          * if this command is returned as the result of the script,
  549.          * then iPtr->scriptFile will go away.
  550.          */
  551.  
  552.         Tcl_SetResult(interp, iPtr->scriptFile, TCL_VOLATILE);
  553.     }
  554.     return TCL_OK;
  555.     } else if ((c == 't') && (strncmp(argv[1], "tclversion", length) == 0)) {
  556.     if (argc != 2) {
  557.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  558.             argv[0], " tclversion\"", (char *) NULL);
  559.         return TCL_ERROR;
  560.     }
  561.  
  562.     /*
  563.      * Note:  TCL_VERSION below is expected to be set with a "-D"
  564.      * switch in the Makefile.
  565.      */
  566.  
  567.     strcpy(iPtr->result, TCL_VERSION);
  568.     return TCL_OK;
  569.     } else if ((c == 'v') && (strncmp(argv[1], "vars", length)) == 0) {
  570.     Tcl_HashTable *tablePtr;
  571.     char *name;
  572.  
  573.     if (argc > 3) {
  574.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  575.             argv[0], " vars [pattern]\"", (char *) NULL);
  576.         return TCL_ERROR;
  577.     }
  578.     if (iPtr->varFramePtr == NULL) {
  579.         tablePtr = &iPtr->globalTable;
  580.     } else {
  581.         tablePtr = &iPtr->varFramePtr->varTable;
  582.     }
  583.     for (hPtr = Tcl_FirstHashEntry(tablePtr, &search);
  584.         hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
  585.         varPtr = (Var *) Tcl_GetHashValue(hPtr);
  586.         if (varPtr->flags & VAR_UNDEFINED) {
  587.         continue;
  588.         }
  589.         name = Tcl_GetHashKey(tablePtr, hPtr);
  590.         if ((argc == 3) && !Tcl_StringMatch(name, argv[2])) {
  591.         continue;
  592.         }
  593.         Tcl_AppendElement(interp, name);
  594.     }
  595.     return TCL_OK;
  596.     } else {
  597.     Tcl_AppendResult(interp, "bad option \"", argv[1],
  598.         "\": should be args, body, cmdcount, commands, ",
  599.         "complete, default, ",
  600.         "exists, globals, level, library, locals, ",
  601.         "patchlevel, procs, script, tclversion, or vars",
  602.         (char *) NULL);
  603.     return TCL_ERROR;
  604.     }
  605. }
  606.  
  607. /*
  608.  *----------------------------------------------------------------------
  609.  *
  610.  * Tcl_JoinCmd --
  611.  *
  612.  *    This procedure is invoked to process the "join" Tcl command.
  613.  *    See the user documentation for details on what it does.
  614.  *
  615.  * Results:
  616.  *    A standard Tcl result.
  617.  *
  618.  * Side effects:
  619.  *    See the user documentation.
  620.  *
  621.  *----------------------------------------------------------------------
  622.  */
  623.  
  624.     /* ARGSUSED */
  625. int
  626. Tcl_JoinCmd(dummy, interp, argc, argv)
  627.     ClientData dummy;            /* Not used. */
  628.     Tcl_Interp *interp;            /* Current interpreter. */
  629.     int argc;                /* Number of arguments. */
  630.     char **argv;            /* Argument strings. */
  631. {
  632.     char *joinString;
  633.     char **listArgv;
  634.     int listArgc, i;
  635.  
  636.     if (argc == 2) {
  637.     joinString = " ";
  638.     } else if (argc == 3) {
  639.     joinString = argv[2];
  640.     } else {
  641.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  642.         " list ?joinString?\"", (char *) NULL);
  643.     return TCL_ERROR;
  644.     }
  645.  
  646.     if (Tcl_SplitList(interp, argv[1], &listArgc, &listArgv) != TCL_OK) {
  647.     return TCL_ERROR;
  648.     }
  649.     for (i = 0; i < listArgc; i++) {
  650.     if (i == 0) {
  651.         Tcl_AppendResult(interp, listArgv[0], (char *) NULL);
  652.     } else  {
  653.         Tcl_AppendResult(interp, joinString, listArgv[i], (char *) NULL);
  654.     }
  655.     }
  656.     ckfree((char *) listArgv);
  657.     return TCL_OK;
  658. }
  659.  
  660. /*
  661.  *----------------------------------------------------------------------
  662.  *
  663.  * Tcl_LindexCmd --
  664.  *
  665.  *    This procedure is invoked to process the "lindex" Tcl command.
  666.  *    See the user documentation for details on what it does.
  667.  *
  668.  * Results:
  669.  *    A standard Tcl result.
  670.  *
  671.  * Side effects:
  672.  *    See the user documentation.
  673.  *
  674.  *----------------------------------------------------------------------
  675.  */
  676.  
  677.     /* ARGSUSED */
  678. int
  679. Tcl_LindexCmd(dummy, interp, argc, argv)
  680.     ClientData dummy;            /* Not used. */
  681.     Tcl_Interp *interp;            /* Current interpreter. */
  682.     int argc;                /* Number of arguments. */
  683.     char **argv;            /* Argument strings. */
  684. {
  685.     char *p, *element, *next;
  686.     int index, size, parenthesized, result, returnLast;
  687.  
  688.     if (argc != 3) {
  689.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  690.         " list index\"", (char *) NULL);
  691.     return TCL_ERROR;
  692.     }
  693.     if ((*argv[2] == 'e') && (strncmp(argv[2], "end", strlen(argv[2])) == 0)) {
  694.     returnLast = 1;
  695.     index = INT_MAX;
  696.     } else {
  697.     returnLast = 0;
  698.     if (Tcl_GetInt(interp, argv[2], &index) != TCL_OK) {
  699.         return TCL_ERROR;
  700.     }
  701.     }
  702.     if (index < 0) {
  703.     return TCL_OK;
  704.     }
  705.     for (p = argv[1] ; index >= 0; index--) {
  706.     result = TclFindElement(interp, p, &element, &next, &size,
  707.         &parenthesized);
  708.     if (result != TCL_OK) {
  709.         return result;
  710.     }
  711.     if ((*next == 0) && returnLast) {
  712.         break;
  713.     }
  714.     p = next;
  715.     }
  716.     if (size == 0) {
  717.     return TCL_OK;
  718.     }
  719.     if (size >= TCL_RESULT_SIZE) {
  720.     interp->result = (char *) ckalloc((unsigned) size+1);
  721.     interp->freeProc = (Tcl_FreeProc *) free;
  722.     }
  723.     if (parenthesized) {
  724.     memcpy((VOID *) interp->result, (VOID *) element, (size_t) size);
  725.     interp->result[size] = 0;
  726.     } else {
  727.     TclCopyAndCollapse(size, element, interp->result);
  728.     }
  729.     return TCL_OK;
  730. }
  731.  
  732. /*
  733.  *----------------------------------------------------------------------
  734.  *
  735.  * Tcl_LinsertCmd --
  736.  *
  737.  *    This procedure is invoked to process the "linsert" Tcl command.
  738.  *    See the user documentation for details on what it does.
  739.  *
  740.  * Results:
  741.  *    A standard Tcl result.
  742.  *
  743.  * Side effects:
  744.  *    See the user documentation.
  745.  *
  746.  *----------------------------------------------------------------------
  747.  */
  748.  
  749.     /* ARGSUSED */
  750. int
  751. Tcl_LinsertCmd(dummy, interp, argc, argv)
  752.     ClientData dummy;            /* Not used. */
  753.     Tcl_Interp *interp;            /* Current interpreter. */
  754.     int argc;                /* Number of arguments. */
  755.     char **argv;            /* Argument strings. */
  756. {
  757.     char *p, *element, savedChar;
  758.     int i, index, count, result, size;
  759.  
  760.     if (argc < 4) {
  761.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  762.         " list index element ?element ...?\"", (char *) NULL);
  763.     return TCL_ERROR;
  764.     }
  765.     if ((*argv[2] == 'e') && (strncmp(argv[2], "end", strlen(argv[2])) == 0)) {
  766.     index = INT_MAX;
  767.     } else if (Tcl_GetInt(interp, argv[2], &index) != TCL_OK) {
  768.     return TCL_ERROR;
  769.     }
  770.  
  771.     /*
  772.      * Skip over the first "index" elements of the list, then add
  773.      * all of those elements to the result.
  774.      */
  775.  
  776.     size = 0;
  777.     element = argv[1];
  778.     for (count = 0, p = argv[1]; (count < index) && (*p != 0); count++) {
  779.     result = TclFindElement(interp, p, &element, &p, &size, (int *) NULL);
  780.     if (result != TCL_OK) {
  781.         return result;
  782.     }
  783.     }
  784.     if (*p == 0) {
  785.     Tcl_AppendResult(interp, argv[1], (char *) NULL);
  786.     } else {
  787.     char *end;
  788.  
  789.     end = element+size;
  790.     if (element != argv[1]) {
  791.         while ((*end != 0) && !isspace(UCHAR(*end))) {
  792.         end++;
  793.         }
  794.     }
  795.     savedChar = *end;
  796.     *end = 0;
  797.     Tcl_AppendResult(interp, argv[1], (char *) NULL);
  798.     *end = savedChar;
  799.     }
  800.  
  801.     /*
  802.      * Add the new list elements.
  803.      */
  804.  
  805.     for (i = 3; i < argc; i++) {
  806.     Tcl_AppendElement(interp, argv[i]);
  807.     }
  808.  
  809.     /*
  810.      * Append the remainder of the original list.
  811.      */
  812.  
  813.     if (*p != 0) {
  814.     Tcl_AppendResult(interp, " ", p, (char *) NULL);
  815.     }
  816.     return TCL_OK;
  817. }
  818.  
  819. /*
  820.  *----------------------------------------------------------------------
  821.  *
  822.  * Tcl_ListCmd --
  823.  *
  824.  *    This procedure is invoked to process the "list" Tcl command.
  825.  *    See the user documentation for details on what it does.
  826.  *
  827.  * Results:
  828.  *    A standard Tcl result.
  829.  *
  830.  * Side effects:
  831.  *    See the user documentation.
  832.  *
  833.  *----------------------------------------------------------------------
  834.  */
  835.  
  836.     /* ARGSUSED */
  837. int
  838. Tcl_ListCmd(dummy, interp, argc, argv)
  839.     ClientData dummy;            /* Not used. */
  840.     Tcl_Interp *interp;            /* Current interpreter. */
  841.     int argc;                /* Number of arguments. */
  842.     char **argv;            /* Argument strings. */
  843. {
  844.     if (argc >= 2) {
  845.     interp->result = Tcl_Merge(argc-1, argv+1);
  846.     interp->freeProc = (Tcl_FreeProc *) free;
  847.     }
  848.     return TCL_OK;
  849. }
  850.  
  851. /*
  852.  *----------------------------------------------------------------------
  853.  *
  854.  * Tcl_LlengthCmd --
  855.  *
  856.  *    This procedure is invoked to process the "llength" Tcl command.
  857.  *    See the user documentation for details on what it does.
  858.  *
  859.  * Results:
  860.  *    A standard Tcl result.
  861.  *
  862.  * Side effects:
  863.  *    See the user documentation.
  864.  *
  865.  *----------------------------------------------------------------------
  866.  */
  867.  
  868.     /* ARGSUSED */
  869. int
  870. Tcl_LlengthCmd(dummy, interp, argc, argv)
  871.     ClientData dummy;            /* Not used. */
  872.     Tcl_Interp *interp;            /* Current interpreter. */
  873.     int argc;                /* Number of arguments. */
  874.     char **argv;            /* Argument strings. */
  875. {
  876.     int count, result;
  877.     char *element, *p;
  878.  
  879.     if (argc != 2) {
  880.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  881.         " list\"", (char *) NULL);
  882.     return TCL_ERROR;
  883.     }
  884.     for (count = 0, p = argv[1]; *p != 0 ; count++) {
  885.     result = TclFindElement(interp, p, &element, &p, (int *) NULL,
  886.         (int *) NULL);
  887.     if (result != TCL_OK) {
  888.         return result;
  889.     }
  890.     if (*element == 0) {
  891.         break;
  892.     }
  893.     }
  894.     sprintf(interp->result, "%d", count);
  895.     return TCL_OK;
  896. }
  897.  
  898. /*
  899.  *----------------------------------------------------------------------
  900.  *
  901.  * Tcl_LrangeCmd --
  902.  *
  903.  *    This procedure is invoked to process the "lrange" Tcl command.
  904.  *    See the user documentation for details on what it does.
  905.  *
  906.  * Results:
  907.  *    A standard Tcl result.
  908.  *
  909.  * Side effects:
  910.  *    See the user documentation.
  911.  *
  912.  *----------------------------------------------------------------------
  913.  */
  914.  
  915.     /* ARGSUSED */
  916. int
  917. Tcl_LrangeCmd(notUsed, interp, argc, argv)
  918.     ClientData notUsed;            /* Not used. */
  919.     Tcl_Interp *interp;            /* Current interpreter. */
  920.     int argc;                /* Number of arguments. */
  921.     char **argv;            /* Argument strings. */
  922. {
  923.     int first, last, result;
  924.     char *begin, *end, c, *dummy, *next;
  925.     int count, firstIsEnd;
  926.  
  927.     if (argc != 4) {
  928.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  929.         " list first last\"", (char *) NULL);
  930.     return TCL_ERROR;
  931.     }
  932.     if ((*argv[2] == 'e') && (strncmp(argv[2], "end", strlen(argv[2])) == 0)) {
  933.     firstIsEnd = 1;
  934.     first = INT_MAX;
  935.     } else {
  936.     firstIsEnd = 0;
  937.     if (Tcl_GetInt(interp, argv[2], &first) != TCL_OK) {
  938.         return TCL_ERROR;
  939.     }
  940.     }
  941.     if (first < 0) {
  942.     first = 0;
  943.     }
  944.     if ((*argv[3] == 'e') && (strncmp(argv[3], "end", strlen(argv[3])) == 0)) {
  945.     last = INT_MAX;
  946.     } else {
  947.     if (Tcl_GetInt(interp, argv[3], &last) != TCL_OK) {
  948.         Tcl_ResetResult(interp);
  949.         Tcl_AppendResult(interp, "expected integer or \"end\" but got \"",
  950.             argv[3], "\"", (char *) NULL);
  951.         return TCL_ERROR;
  952.     }
  953.     }
  954.     if ((first > last) && !firstIsEnd) {
  955.     return TCL_OK;
  956.     }
  957.  
  958.     /*
  959.      * Extract a range of fields.
  960.      */
  961.  
  962.     for (count = 0, begin = argv[1]; count < first; begin = next, count++) {
  963.     result = TclFindElement(interp, begin, &dummy, &next, (int *) NULL,
  964.         (int *) NULL);
  965.     if (result != TCL_OK) {
  966.         return result;
  967.     }
  968.     if (*next == 0) {
  969.         if (firstIsEnd) {
  970.         first = count;
  971.         } else {
  972.         begin = next;
  973.         }
  974.         break;
  975.     }
  976.     }
  977.     for (count = first, end = begin; (count <= last) && (*end != 0);
  978.         count++) {
  979.     result = TclFindElement(interp, end, &dummy, &end, (int *) NULL,
  980.         (int *) NULL);
  981.     if (result != TCL_OK) {
  982.         return result;
  983.     }
  984.     }
  985.     if (end == begin) {
  986.     return TCL_OK;
  987.     }
  988.  
  989.     /*
  990.      * Chop off trailing spaces.
  991.      */
  992.  
  993.     while (isspace(UCHAR(end[-1]))) {
  994.     end--;
  995.     }
  996.     c = *end;
  997.     *end = 0;
  998.     Tcl_SetResult(interp, begin, TCL_VOLATILE);
  999.     *end = c;
  1000.     return TCL_OK;
  1001. }
  1002.  
  1003. /*
  1004.  *----------------------------------------------------------------------
  1005.  *
  1006.  * Tcl_LreplaceCmd --
  1007.  *
  1008.  *    This procedure is invoked to process the "lreplace" Tcl command.
  1009.  *    See the user documentation for details on what it does.
  1010.  *
  1011.  * Results:
  1012.  *    A standard Tcl result.
  1013.  *
  1014.  * Side effects:
  1015.  *    See the user documentation.
  1016.  *
  1017.  *----------------------------------------------------------------------
  1018.  */
  1019.  
  1020.     /* ARGSUSED */
  1021. int
  1022. Tcl_LreplaceCmd(notUsed, interp, argc, argv)
  1023.     ClientData notUsed;            /* Not used. */
  1024.     Tcl_Interp *interp;            /* Current interpreter. */
  1025.     int argc;                /* Number of arguments. */
  1026.     char **argv;            /* Argument strings. */
  1027. {
  1028.     char *p1, *p2, *element, savedChar, *dummy, *next;
  1029.     int i, first, last, count, result, size, firstIsEnd;
  1030.  
  1031.     if (argc < 4) {
  1032.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1033.         " list first last ?element element ...?\"", (char *) NULL);
  1034.     return TCL_ERROR;
  1035.     }
  1036.     if ((*argv[2] == 'e') && (strncmp(argv[2], "end", strlen(argv[2])) == 0)) {
  1037.     firstIsEnd = 1;
  1038.     first = INT_MAX;
  1039.     } else {
  1040.     firstIsEnd = 0;
  1041.     if (Tcl_GetInt(interp, argv[2], &first) != TCL_OK) {
  1042.         return TCL_ERROR;
  1043.     }
  1044.     }
  1045.     if ((*argv[3] == 'e') && (strncmp(argv[3], "end", strlen(argv[3])) == 0)) {
  1046.     last = INT_MAX;
  1047.     } else {
  1048.     if (TclGetListIndex(interp, argv[3], &last) != TCL_OK) {
  1049.         return TCL_ERROR;
  1050.     }
  1051.     }
  1052.     if (first < 0) {
  1053.     first = 0;
  1054.     }
  1055.     if (last < 0) {
  1056.     last = 0;
  1057.     }
  1058.  
  1059.     /*
  1060.      * Skip over the elements of the list before "first".
  1061.      */
  1062.  
  1063.     size = 0;
  1064.     element = argv[1];
  1065.     for (count = 0, p1 = argv[1]; (count < first) && (*p1 != 0); count++) {
  1066.     result = TclFindElement(interp, p1, &element, &next, &size,
  1067.         (int *) NULL);
  1068.     if (result != TCL_OK) {
  1069.         return result;
  1070.     }
  1071.     if ((*next == 0) && firstIsEnd) {
  1072.         break;
  1073.     }
  1074.     p1 = next;
  1075.     }
  1076.     if (*p1 == 0) {
  1077.     Tcl_AppendResult(interp, "list doesn't contain element ",
  1078.         argv[2], (char *) NULL);
  1079.     return TCL_ERROR;
  1080.     }
  1081.     if (count > last) {
  1082.     Tcl_AppendResult(interp, "first index must not be greater than second",
  1083.         (char *) NULL);
  1084.     return TCL_ERROR;
  1085.     }
  1086.  
  1087.     /*
  1088.      * Skip over the elements of the list up through "last".
  1089.      */
  1090.  
  1091.     for (p2 = p1 ; (count <= last) && (*p2 != 0); count++) {
  1092.     result = TclFindElement(interp, p2, &dummy, &p2, (int *) NULL,
  1093.         (int *) NULL);
  1094.     if (result != TCL_OK) {
  1095.         return result;
  1096.     }
  1097.     }
  1098.  
  1099.     /*
  1100.      * Add the elements before "first" to the result.  Drop any terminating
  1101.      * white space, since a separator will be added below, if needed.
  1102.      */
  1103.  
  1104.     while ((p1 != argv[1]) && (isspace(UCHAR(p1[-1])))) {
  1105.     p1--;
  1106.     }
  1107.     savedChar = *p1;
  1108.     *p1 = 0;
  1109.     Tcl_AppendResult(interp, argv[1], (char *) NULL);
  1110.     *p1 = savedChar;
  1111.  
  1112.     /*
  1113.      * Add the new list elements.
  1114.      */
  1115.  
  1116.     for (i = 4; i < argc; i++) {
  1117.     Tcl_AppendElement(interp, argv[i]);
  1118.     }
  1119.  
  1120.     /*
  1121.      * Append the remainder of the original list.
  1122.      */
  1123.  
  1124.     if (*p2 != 0) {
  1125.     if (*interp->result == 0) {
  1126.         Tcl_SetResult(interp, p2, TCL_VOLATILE);
  1127.     } else {
  1128.         Tcl_AppendResult(interp, " ", p2, (char *) NULL);
  1129.     }
  1130.     }
  1131.     return TCL_OK;
  1132. }
  1133.  
  1134. /*
  1135.  *----------------------------------------------------------------------
  1136.  *
  1137.  * Tcl_LsearchCmd --
  1138.  *
  1139.  *    This procedure is invoked to process the "lsearch" Tcl command.
  1140.  *    See the user documentation for details on what it does.
  1141.  *
  1142.  * Results:
  1143.  *    A standard Tcl result.
  1144.  *
  1145.  * Side effects:
  1146.  *    See the user documentation.
  1147.  *
  1148.  *----------------------------------------------------------------------
  1149.  */
  1150.  
  1151.     /* ARGSUSED */
  1152. int
  1153. Tcl_LsearchCmd(notUsed, interp, argc, argv)
  1154.     ClientData notUsed;            /* Not used. */
  1155.     Tcl_Interp *interp;            /* Current interpreter. */
  1156.     int argc;                /* Number of arguments. */
  1157.     char **argv;            /* Argument strings. */
  1158. {
  1159. #define EXACT    0
  1160. #define GLOB    1
  1161. #define REGEXP    2
  1162.     int listArgc;
  1163.     char **listArgv;
  1164.     int i, match, mode, index;
  1165.  
  1166.     mode = GLOB;
  1167.     if (argc == 4) {
  1168.     if (strcmp(argv[1], "-exact") == 0) {
  1169.         mode = EXACT;
  1170.     } else if (strcmp(argv[1], "-glob") == 0) {
  1171.         mode = GLOB;
  1172.     } else if (strcmp(argv[1], "-regexp") == 0) {
  1173.         mode = REGEXP;
  1174.     } else {
  1175.         Tcl_AppendResult(interp, "bad search mode \"", argv[1],
  1176.             "\": must be -exact, -glob, or -regexp", (char *) NULL);
  1177.         return TCL_ERROR;
  1178.     }
  1179.     } else if (argc != 3) {
  1180.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1181.         " ?mode? list pattern\"", (char *) NULL);
  1182.     return TCL_ERROR;
  1183.     }
  1184.     if (Tcl_SplitList(interp, argv[argc-2], &listArgc, &listArgv) != TCL_OK) {
  1185.     return TCL_ERROR;
  1186.     }
  1187.     index = -1;
  1188.     for (i = 0; i < listArgc; i++) {
  1189.     match = 0;
  1190.     switch (mode) {
  1191.         case EXACT:
  1192.         match = (strcmp(listArgv[i], argv[argc-1]) == 0);
  1193.         break;
  1194.         case GLOB:
  1195.         match = Tcl_StringMatch(listArgv[i], argv[argc-1]);
  1196.         break;
  1197.         case REGEXP:
  1198.         match = Tcl_RegExpMatch(interp, listArgv[i], argv[argc-1]);
  1199.         if (match < 0) {
  1200.             ckfree((char *) listArgv);
  1201.             return TCL_ERROR;
  1202.         }
  1203.         break;
  1204.     }
  1205.     if (match) {
  1206.         index = i;
  1207.         break;
  1208.     }
  1209.     }
  1210.     sprintf(interp->result, "%d", index);
  1211.     ckfree((char *) listArgv);
  1212.     return TCL_OK;
  1213. }
  1214.  
  1215. /*
  1216.  *----------------------------------------------------------------------
  1217.  *
  1218.  * Tcl_LsortCmd --
  1219.  *
  1220.  *    This procedure is invoked to process the "lsort" Tcl command.
  1221.  *    See the user documentation for details on what it does.
  1222.  *
  1223.  * Results:
  1224.  *    A standard Tcl result.
  1225.  *
  1226.  * Side effects:
  1227.  *    See the user documentation.
  1228.  *
  1229.  *----------------------------------------------------------------------
  1230.  */
  1231.  
  1232.     /* ARGSUSED */
  1233. int
  1234. Tcl_LsortCmd(notUsed, interp, argc, argv)
  1235.     ClientData notUsed;            /* Not used. */
  1236.     Tcl_Interp *interp;            /* Current interpreter. */
  1237.     int argc;                /* Number of arguments. */
  1238.     char **argv;            /* Argument strings. */
  1239. {
  1240.     int listArgc, i, c;
  1241.     size_t length;
  1242.     char **listArgv;
  1243.     char *command = NULL;        /* Initialization needed only to
  1244.                      * prevent compiler warning. */
  1245.  
  1246.     if (argc < 2) {
  1247.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1248.         " ?-ascii? ?-integer? ?-real? ?-increasing? ?-decreasing?",
  1249.         " ?-command string? list\"", (char *) NULL);
  1250.     return TCL_ERROR;
  1251.     }
  1252.  
  1253.     /*
  1254.      * Parse arguments to set up the mode for the sort.
  1255.      */
  1256.  
  1257.     sortInterp = interp;
  1258.     sortMode = ASCII;
  1259.     sortIncreasing = 1;
  1260.     sortCode = TCL_OK;
  1261.     for (i = 1; i < argc-1; i++) {
  1262.     length = strlen(argv[i]);
  1263.     if (length < 2) {
  1264.         badSwitch:
  1265.         Tcl_AppendResult(interp, "bad switch \"", argv[i],
  1266.             "\": must be -ascii, -integer, -real, -increasing",
  1267.             " -decreasing, or -command", (char *) NULL);
  1268.         return TCL_ERROR;
  1269.     }
  1270.     c = argv[i][1];
  1271.     if ((c == 'a') && (strncmp(argv[i], "-ascii", length) == 0)) {
  1272.         sortMode = ASCII;
  1273.     } else if ((c == 'c') && (strncmp(argv[i], "-command", length) == 0)) {
  1274.         if (i == argc-2) {
  1275.         Tcl_AppendResult(interp, "\"-command\" must be",
  1276.             " followed by comparison command", (char *) NULL);
  1277.         return TCL_ERROR;
  1278.         }
  1279.         sortMode = COMMAND;
  1280.         command = argv[i+1];
  1281.         i++;
  1282.     } else if ((c == 'd')
  1283.         && (strncmp(argv[i], "-decreasing", length) == 0)) {
  1284.         sortIncreasing = 0;
  1285.     } else if ((c == 'i') && (length >= 4)
  1286.         && (strncmp(argv[i], "-increasing", length) == 0)) {
  1287.         sortIncreasing = 1;
  1288.     } else if ((c == 'i') && (length >= 4)
  1289.         && (strncmp(argv[i], "-integer", length) == 0)) {
  1290.         sortMode = INTEGER;
  1291.     } else if ((c == 'r')
  1292.         && (strncmp(argv[i], "-real", length) == 0)) {
  1293.         sortMode = REAL;
  1294.     } else {
  1295.         goto badSwitch;
  1296.     }
  1297.     }
  1298.     if (sortMode == COMMAND) {
  1299.     Tcl_DStringInit(&sortCmd);
  1300.     Tcl_DStringAppend(&sortCmd, command, -1);
  1301.     }
  1302.  
  1303.     if (Tcl_SplitList(interp, argv[argc-1], &listArgc, &listArgv) != TCL_OK) {
  1304.     return TCL_ERROR;
  1305.     }
  1306.     qsort((VOID *) listArgv, (size_t) listArgc, sizeof (char *),
  1307.         SortCompareProc);
  1308.     if (sortCode == TCL_OK) {
  1309.     Tcl_ResetResult(interp);
  1310.     interp->result = Tcl_Merge(listArgc, listArgv);
  1311.     interp->freeProc = (Tcl_FreeProc *) free;
  1312.     }
  1313.     if (sortMode == COMMAND) {
  1314.     Tcl_DStringFree(&sortCmd);
  1315.     }
  1316.     ckfree((char *) listArgv);
  1317.     return sortCode;
  1318. }
  1319.  
  1320. /*
  1321.  *----------------------------------------------------------------------
  1322.  *
  1323.  * SortCompareProc --
  1324.  *
  1325.  *    This procedure is invoked by qsort to determine the proper
  1326.  *    ordering between two elements.
  1327.  *
  1328.  * Results:
  1329.  *    < 0 means first is "smaller" than "second", > 0 means "first"
  1330.  *    is larger than "second", and 0 means they should be treated
  1331.  *    as equal.
  1332.  *
  1333.  * Side effects:
  1334.  *    None, unless a user-defined comparison command does something
  1335.  *    weird.
  1336.  *
  1337.  *----------------------------------------------------------------------
  1338.  */
  1339.  
  1340. static int
  1341. SortCompareProc(first, second)
  1342.     CONST VOID *first, *second;        /* Elements to be compared. */
  1343. {
  1344.     int order;
  1345.     char *firstString = *((char **) first);
  1346.     char *secondString = *((char **) second);
  1347.  
  1348.     order = 0;
  1349.     if (sortCode != TCL_OK) {
  1350.     /*
  1351.      * Once an error has occurred, skip any future comparisons
  1352.      * so as to preserve the error message in sortInterp->result.
  1353.      */
  1354.  
  1355.     return order;
  1356.     }
  1357.     if (sortMode == ASCII) {
  1358.     order = strcmp(firstString, secondString);
  1359.     } else if (sortMode == INTEGER) {
  1360.     int a, b;
  1361.  
  1362.     if ((Tcl_GetInt(sortInterp, firstString, &a) != TCL_OK)
  1363.         || (Tcl_GetInt(sortInterp, secondString, &b) != TCL_OK)) {
  1364.         Tcl_AddErrorInfo(sortInterp,
  1365.             "\n    (converting list element from string to integer)");
  1366.         sortCode = TCL_ERROR;
  1367.         return order;
  1368.     }
  1369.     if (a > b) {
  1370.         order = 1;
  1371.     } else if (b > a) {
  1372.         order = -1;
  1373.     }
  1374.     } else if (sortMode == REAL) {
  1375.     double a, b;
  1376.  
  1377.     if ((Tcl_GetDouble(sortInterp, firstString, &a) != TCL_OK)
  1378.         || (Tcl_GetDouble(sortInterp, secondString, &b) != TCL_OK)) {
  1379.         Tcl_AddErrorInfo(sortInterp,
  1380.             "\n    (converting list element from string to real)");
  1381.         sortCode = TCL_ERROR;
  1382.         return order;
  1383.     }
  1384.     if (a > b) {
  1385.         order = 1;
  1386.     } else if (b > a) {
  1387.         order = -1;
  1388.     }
  1389.     } else {
  1390.     int oldLength;
  1391.     char *end;
  1392.  
  1393.     /*
  1394.      * Generate and evaluate a command to determine which string comes
  1395.      * first.
  1396.      */
  1397.  
  1398.     oldLength = Tcl_DStringLength(&sortCmd);
  1399.     Tcl_DStringAppendElement(&sortCmd, firstString);
  1400.     Tcl_DStringAppendElement(&sortCmd, secondString);
  1401.     sortCode = Tcl_Eval(sortInterp, Tcl_DStringValue(&sortCmd));
  1402.     Tcl_DStringTrunc(&sortCmd, oldLength);
  1403.     if (sortCode != TCL_OK) {
  1404.         Tcl_AddErrorInfo(sortInterp,
  1405.             "\n    (user-defined comparison command)");
  1406.         return order;
  1407.     }
  1408.  
  1409.     /*
  1410.      * Parse the result of the command.
  1411.      */
  1412.  
  1413.     order = strtol(sortInterp->result, &end, 0);
  1414.     if ((end == sortInterp->result) || (*end != 0)) {
  1415.         Tcl_ResetResult(sortInterp);
  1416.         Tcl_AppendResult(sortInterp,
  1417.             "comparison command returned non-numeric result",
  1418.             (char *) NULL);
  1419.         sortCode = TCL_ERROR;
  1420.         return order;
  1421.     }
  1422.     }
  1423.     if (!sortIncreasing) {
  1424.     order = -order;
  1425.     }
  1426.     return order;
  1427. }
  1428.